$(function () {
    var colModelArray = [
        {label: '需求车间', name: 'workshopName', index: 'WORKSHOP', width: 60},
        {label: '类别', name: 'type', index: 'TYPE', width: 60},
        {label: '物资名称', name: 'name', index: 'NAME'},
        {label: '规格型号（图号）', name: 'mapNo', index: 'MAP_NO'},
        {label: '条码代码', name: 'uniqueBarCode', index: 'UNIQUE_BAR_CODE', width: 60},
        {label: '中标厂家', name: 'supplierName', index: 'SUPPLIER', width: 80},
        {label: '中标单价', name: 'unitPrice', index: 'UNIT_PRICE', width: 60},
        {label: '库存', name: 'deptStockNum', index: 'MSDS.STOCK_NUM', width: 60},
        {label: '在途', name: 'deptTransitNum', index: 'MSDS.TRANSIT_NUM', width: 60,required:"true"},
        {label: '需求数<font style="color:red;" face="宋体">*</font>', name: 'demandNum', index: 'DEMAND_NUM', width: 60 },
        {label: '预计到货时间<font style="color:red;" face="宋体">*</font>', name: 'eta', index: 'ETA', width: 80},
        {label: '中标厂家', name: 'supplier', hidden: true},
        {label: '需求车间', name: 'workshop', hidden: true},
        {label: '模具型芯ID', name: 'moldSpId', hidden: true},
        {label: '型芯计划ID', name: 'planId', hidden: true},
        {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
    ];

    var planId = getQueryString('planId');
    if(!planId){
        colModelArray[9].editable = true;
        colModelArray[9].editrules = {integer:true, minValue:1, required:true};
        colModelArray[10].editable = true;
        colModelArray[10].editrules = {date:true,required:true};
    }else{
        vm.updatable = false;
    }
    $("#jqGrid").Grid({
        url: `../modulespdemanditem/list?planId=${planId}`,
        colModel: colModelArray,
        forceFit: true,
        cellEdit: true,
        cellsubmit: 'remote',
        cellurl: '../modulespdemanditem/adjust'
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        updatable: true,
        title: null,
        moduleSpDemandItem: {
            remark: '',
            modifyUser: '',
            gmtModified: '',
            createUser: '',
            gmtCreate: '',
            demandMonth: '',
            eta: '',
            demandNum: '',
            unitPrice: '',
            supplier: '',
            workshop: '',
            moldSpId: '',
            planId: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        generate() {
            confirm('确定要根据调整后的型芯需求名称生成型芯计划吗？', function () {
                // 根据型芯需求明细 生成制作单据
                Ajax.request({
                    url: "../modulespdemanditem/generate",
                    async: true,
                    successCallback: function (r) {
                        alert('生成成功，请前往型芯计划进行查看！', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            let url = vm.moduleSpDemandItem.id == (null || '') ? "../modulespdemanditem/save" : "../modulespdemanditem/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleSpDemandItem),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulespdemanditem/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulespdemanditem/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleSpDemandItem = r.moduleSpDemandItem;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        }
    }
});
